package edu.xidian.bdilab.jrbigdata.service.enterprise.register.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.enterprise.EnterpriseCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseUser;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseUserMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.register.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName RegisterServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/25 上午9:31
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    EnterpriseUserMapper enterpriseUserMapper;


    /**
     * 查询用户是否已经注册
     * @return
     */
    @Override
    public boolean checkEnterpriseInfo(String social_code, String enterprise_name,
                                              int registered_captial, String owner_name,
                                              int region, String street, String owner_phone,
                                              String contact_name, String contact_phone,
                                              Date establish_time, String establish_address,
                                              Integer fund_quota, Integer available, String business_scop) {

        QueryWrapper<EnterpriseInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("social_code",social_code);

        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(queryWrapper);
        if (enterpriseInfo == null){
            return true;

    //                setEnterpriseInfo(social_code, enterprise_name, registered_captial,
    //                        owner_name, region, street, owner_phone, contact_name, contact_phone, establish_time,
    //                        establish_address, fund_quota, available, business_scop);
    //
    //                responseResult.setSuccess(true);
    //                responseResult.setCode(EnterpriseCode.REGISTER_SUCCESS.getCode());
    //                responseResult.setMessage(EnterpriseCode.REGISTER_SUCCESS.getMessage());
        }
        return false;
    }

    /**
     * 信息插入数据库
     */
    @Override
    public void setEnterpriseInfo(String social_code, String enterprise_name,
                                 int registered_captial, String owner_name,
                                 int region, String street, String owner_phone,
                                 String contact_name, String contact_phone,
                                 Date establish_time, String establish_address,
                                 Integer fund_quota, Integer available,
                                 String business_scop, String enterprise_desc) {

        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();

        enterpriseInfo.setSocialCode(social_code);
        enterpriseInfo.setEnterpriseName(enterprise_name);
        enterpriseInfo.setRegisteredCaptial(registered_captial);
        enterpriseInfo.setOwnerName(owner_name);
        enterpriseInfo.setRegion(region);
        enterpriseInfo.setStreet(street);
        enterpriseInfo.setOwnerPhone(owner_phone);
        enterpriseInfo.setContactName(contact_name);
        enterpriseInfo.setContactPhone(contact_phone);
        enterpriseInfo.setEstablishTime(establish_time);
        enterpriseInfo.setEstablishAddress(establish_address);
        if (fund_quota != null ){
            enterpriseInfo.setFundQuota(fund_quota);
        }
        if( available != null){
            enterpriseInfo.setAvailable(available);
        }
        if( enterprise_desc != null&&enterprise_desc.length()!=0){
            enterpriseInfo.setEnterpriseDesc(enterprise_desc);
        }
        else{
            enterpriseInfo.setEnterpriseDesc("该企业暂无详情");
        }
        enterpriseInfo.setBusinessScop(business_scop);
        enterpriseInfoMapper.insert(enterpriseInfo);
    }

    /**
     * 将注册企业添加至企业用户表中
     * @param social_code
     * @param password
     */
    @Override
    public void addEnterpriseUser(String social_code, String password) {
        EnterpriseUser enterpriseUser = new EnterpriseUser();
        enterpriseUser.setPassword(password);
        enterpriseUser.setUserName(social_code);
        enterpriseUserMapper.insert(enterpriseUser);
    }
}
